package drops.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;
import java.util.Calendar;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SearchTransactionPanel extends JPanel implements  ActionListener, ItemListener{
	DateSelectorPanel dateField;
	JTextField textField;
	JFormattedTextField numField;
	JComboBox categoryDropDown;
	ComboBoxModel categoryModel;
	JButton searchButton;
	MainFrame mainFrame;
	
	public SearchTransactionPanel (MainFrame mainFrame) {
		super();
		this.mainFrame = mainFrame;
		
		searchButton = new JButton("Search");
		searchButton.addActionListener(this);
		
		String[] categories = {"Transaction Number", "Patient Number", "Description", "Date"};
		categoryModel = new DefaultComboBoxModel(categories);
		categoryDropDown = new JComboBox(categoryModel);
		
		textField = new JTextField(10);
		textField.setVisible(false);
		dateField = new DateSelectorPanel(Calendar.getInstance().getTime());
		dateField.setVisible(false);
		numField = new JFormattedTextField(NumberFormat.getNumberInstance());
		numField.setValue(0);
		numField.setColumns(10);
		numField.setVisible(true);
		add(categoryDropDown);
		categoryDropDown.addItemListener(this);
		add(textField);
		add(dateField);
		add(numField);
		add(searchButton);
	}
	
	public void search(boolean b) {
		String category = categoryModel.getSelectedItem().toString();
		if (category=="Date") {
			mainFrame.searchTransaction(dateField.getDate(), category);
		}
		else if (category=="Patient Number"||category=="Transaction Number") {
			mainFrame.searchTransaction(numField.getValue().toString(), category);
		}
		else {
			mainFrame.searchTransaction(textField.getText(), categoryModel.getSelectedItem().toString());
		}
		if (b) {
			mainFrame.transactionFrame();
		}
	}

	public void updateField() {
		String category = categoryModel.getSelectedItem().toString();
		if (category=="Date") {
			if (!dateField.isVisible()) {
				dateField.setVisible(true);
				textField.setVisible(false);
				numField.setVisible(false);
			}
		}
		else if (category=="Patient Number"||category=="Transaction Number") {
			if (!numField.isVisible()) {
				numField.setVisible(true);
				dateField.setVisible(false);
				textField.setVisible(false);
			}
		}
		else {
			if (!textField.isVisible()) {
				textField.setVisible(true);
				numField.setVisible(false);
				dateField.setVisible(false);
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		search(true);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		updateField();
	}
}
